
package com.eltonkent.db;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.eltonkent.ui.containers.SimpleIconListContainer;
import com.eltonkent.ui.containers.TemplateFieldContainer;
import com.eltonkent.util.DateUtil;

public class CustomTemplateTable extends GenericTable
{

	private final static String TABLE_NAME = "TBL_CUSTOM_TEMPLATE";
	private final static String COL_TEMPLATE_NAME = "TEMPLATE_NAME";
	private final static String COL_CUSTOM_ENTRIES = "CUSTOM_ENTRY";
	private final static String COL_CREATED_ON = "CREATED_ON";
	private final static String COL_ICON_ID = "ICON_ID";

	CustomTemplateTable()
	{
	}

	public CustomTemplateTable(Context context)
	{
		super(context);
	}

	public void addEntry(String templateName, ArrayList<TemplateFieldContainer> entryFieldList, String icon)
	{
		ContentValues values = new ContentValues();
		values.put(COL_CREATED_ON, DateUtil.getFormattedDate(new Date(System.currentTimeMillis())));
		values.put(COL_CUSTOM_ENTRIES, serializeList(entryFieldList));
		values.put(COL_TEMPLATE_NAME, templateName);
		values.put(COL_ICON_ID, icon);
		getDatabase().insert(TABLE_NAME, null, values);
		closeDB();
	}

	@Override
	void doAfterTableCreate(SQLiteDatabase database)
	{
	}

	public ArrayList<SimpleIconListContainer> getAllEntries()
	{
		Cursor data = getDatabase().query(TABLE_NAME, new String[]
		{ COL_TEMPLATE_NAME, COL_ICON_ID }, null, null, null, null, null);
		data.moveToFirst();
		ArrayList<SimpleIconListContainer> listData = new ArrayList<SimpleIconListContainer>();
		int idx1 = data.getColumnIndex(COL_TEMPLATE_NAME);
		int idx2 = data.getColumnIndex(COL_ICON_ID);
		for(int i = 0; i < data.getCount(); i++)
		{
			SimpleIconListContainer info = new SimpleIconListContainer(data.getString(idx1), data
					.getString(idx2));
			listData.add(info);
			data.moveToNext();
		}
		data.close();
		closeDB();
		return listData;
	}

	public ArrayList<TemplateFieldContainer> getEntry(String entryName)
	{
		Cursor data = getDatabase().query(TABLE_NAME, new String[]
		{ COL_CUSTOM_ENTRIES }, COL_TEMPLATE_NAME + "='" + entryName + "'", null, null, null, null, null);
		data.moveToFirst();
		byte[] containerData = data.getBlob(data.getColumnIndex(COL_CUSTOM_ENTRIES));
		ByteArrayInputStream bis = new ByteArrayInputStream(containerData);
		DataInputStream dis = new DataInputStream(bis);
		int count = 0;
		ArrayList<TemplateFieldContainer> containerList = new ArrayList<TemplateFieldContainer>();
		try
		{
			count = dis.readInt();
			for(int i = 0; i < count; i++)
			{
				containerList.add(TemplateFieldContainer.readFromStream(dis));
			}
		} catch(IOException e)
		{
			e.printStackTrace();
		}
		data.close();
		return containerList;
	}

	@Override
	List<Field> getTableFields()
	{
		List<Field> fields = new ArrayList<Field>();
		fields.add(new Field(COL_TEMPLATE_NAME, "text"));
		fields.add(new Field(COL_CUSTOM_ENTRIES, "blob"));
		fields.add(new Field(COL_CREATED_ON, "text"));
		fields.add(new Field(COL_ICON_ID, "text"));
		return fields;
	}

	@Override
	String getTableName()
	{
		return TABLE_NAME;
	}

	public boolean hasEntry(String templateName)
	{
		Cursor data = getDatabase().query(TABLE_NAME, new String[]
		{ COL_TEMPLATE_NAME }, null, null, null, null, null);
		data.moveToFirst();
		int idx = data.getColumnIndex(COL_TEMPLATE_NAME);
		for(int i = 0; i < data.getCount(); i++)
		{
			if(templateName.equals(data.getString(idx)))
				return true;
			data.moveToNext();
		}
		data.close();
		closeDB();
		return false;
	}

	public void deleteTemplate(String templateName)
	{
		getDatabase().delete(TABLE_NAME, COL_TEMPLATE_NAME + "='" + templateName + "'", null);
		closeDB();
	}

	private byte[] serializeList(ArrayList<TemplateFieldContainer> fields)
	{
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(bos);
		try
		{
			int size = fields.size();
			dos.writeInt(size);
			for(int i = 0; i < size; i++)
			{
				fields.get(i).writeToStream(dos);
			}
		} catch(IOException e)
		{
			e.printStackTrace();
		}
		Log.d("DB", "Array Length " + bos.toByteArray().length);
		return bos.toByteArray();
	}

	@Override
	public void deSerialize(DataInputStream dis) throws IOException
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void serialize(DataOutputStream dos) throws IOException
	{
		// TODO Auto-generated method stub

	}

}
